package pt.inescid.components.policyengine.heimdall.pap.base;

import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.EventRelevance;

public class CesAggDefinition extends SimpleDefinition {

	private CesDefinition ces = null;
	private DomainDefinition domain = null;
	private EventFieldDefinition eventField = null;
	
	public CesAggDefinition(int type, String valueType,
			RuleDefinition previousLevel, 
			String domainName, String cesName, String value,
			ModelDefinition currentModelDef) {
		super(type, valueType, previousLevel, value);
		this.ces = currentModelDef.getCes(cesName);
		this.domain = currentModelDef.getDomain(domainName);
		this.eventField = null;
	}

//  ----------------------------------------------------------
//  ------------------------   ...EventField   ------------------------
//  ----------------------------------------------------------
	public void addEventField(EventFieldDefinition eventField) {
		this.eventField = eventField;
	}

	public EventFieldDefinition getEventField() {
		return eventField;
	}

//  ----------------------------------------------------------
//  ------------------------   toString   ------------------------
//  ----------------------------------------------------------
	public String toString() {
		String result = "";
		
		result = RULE_TYPE_CODES[type] + "(" + 
				ces.getName() + "[" + domain.getName() + 
				"])." + eventField.getValue();
		
		return (result);
	}

//  ----------------------------------------------------------
//  ------------------------   getFunction   ------------------------
//  ----------------------------------------------------------
	public String getFunction() {
		String result = "";
		
		result += 
"		CesInstance " + getTemporaryVar() +
"A = currentModel.getCes(\n" +
"				\"" + domain.getName() + "\", \"" + 
ces.getName() + "\");\n" +
"		Vector " + getTemporaryVar() +
"B = " + getTemporaryVar() +
"A.getUnprocessedEvents();\n" +
"		int " + getTemporaryVar() + " = " + getTemporaryVar() +
"A.getValue();\n" +
"		for (int i = 0; i < " + getTemporaryVar() +
"B.size(); i++) {\n" +
eventField.getFunction() + 
"			" + getTemporaryVar() + " += " +
eventField.getTemporaryVar() + ";\n" +
"		};\n";
		
		return (result);
	}

//  ----------------------------------------------------------
//  ------------------------   getRelevantEventFields   ------------------------
//  ----------------------------------------------------------
	public Vector getRelevantEventFields() {
		EventRelevance newField = new EventRelevance(
				ces.getModelDef(),
				ces.getName(),
				domain.getName(),
				eventField.getValue(),
				eventField.getValueType(),
				domain.isSingleNodeDomain());
		
		Vector result = new Vector();
		result.add(newField);
		
		return (result);
	} // getRelevantEventFields

	
//  ----------------------------------------------------------
//  ------------------------   getRelevantCes   ------------------------
//  ----------------------------------------------------------
	public Vector getRelevantCes() {
		CesDefinition newCes = ces;
		
		Vector result = new Vector();
		result.add(newCes);
		
		return (result);
	} // getRelevantCes

	
//  ----------------------------------------------------------
//  ------------------------   keepOnlyThisCes   ------------------------
//  ----------------------------------------------------------
	public RuleDefinition keepOnlyThisCes(String cesName) {
		if (ces.getName().equals(cesName)) {
			return (this);
		}
		else {
			switch (type) {
			case RuleDefinition.INT_VALUE:
				return (new SimpleDefinition(
						RuleDefinition.LITERAL_EXPRESSION,
						RuleDefinition.EXPRESSION_VALUE_CODES[
						        RuleDefinition.INT_VALUE],
						previousLevel,
						"0"));
			default:
				break;
			}
		}
		
		return (null);
	} // keepOnlyThisCes

//  ----------------------------------------------------------
//  ------------------------   getParameters   ------------------------
//  ----------------------------------------------------------
	public Vector getParameters() {
		Vector allParameters = new Vector();
		return (allParameters);
	} // getParameters

	public CesDefinition getCes() {
		return ces;
	}

	public DomainDefinition getDomain() {
		return domain;
	}

	public void setCes(CesDefinition ces) {
		this.ces = ces;
	}

	public void setDomain(DomainDefinition domain) {
		this.domain = domain;
	}

	public void setEventField(EventFieldDefinition eventField) {
		this.eventField = eventField;
	}

}; // SubPolicyExpression
